import java.io.InputStream;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class ConexionMidlet extends MIDlet implements CommandListener{

	StringItem lblparcial, lblfinal, lblobs;
	Display ven;
	public static ConexionMidlet midlet; //referencia estatica al MIDlet
	Form frmconsulta; 
	TextField txtcod;
	Command btnok;
	
	
	public ConexionMidlet() {
		frmconsulta = new Form("Formulario notas");
		txtcod = new TextField("Codigo", null, 10, TextField.ANY);
		lblparcial = new StringItem("nota prcial", "");
		lblfinal = new StringItem("nota final", "");
		lblobs = new StringItem("Estado", "");
		btnok = new Command("Consjultar", Command.OK, 0);
		frmconsulta.append(txtcod);
		frmconsulta.append(lblparcial);
		frmconsulta.append(lblfinal);
		frmconsulta.append(lblobs);
		frmconsulta.addCommand(btnok);
		frmconsulta.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {
		ven = Display.getDisplay(this);
		
		midlet = this;
		ven.setCurrent(frmconsulta);
	}
	
	public void Conectarse(){
        try{
            String url="http://localhost:83/AppCooperativaDWP/ConsultaPrueba.jsp";
            HttpConnection http=null;
           
            http=(HttpConnection)Connector.open(url);
            http.setRequestMethod(HttpConnection.GET);
           
            InputStream flujo=null;
            flujo= http.openInputStream();
            int car;
            StringBuffer cadena =new StringBuffer();
           
            while((car =flujo.read())!= -1){
                cadena.append((char)car);               
            }//fin while
            
			lblparcial.setText(cadena.toString());
        }// fin try
        catch(Exception e){
            System.out.println(e.getMessage());
        }
}

	public void commandAction(Command c, Displayable arg1) {
		if(c == btnok){
          Thread hilo= new Thread(){
              public void run()
	              {   Conectarse();            
	              }
          	   };
          hilo.start();         
      }//fin if		
	}
	
	
}
